package com.richard.library.basic.util.media;

import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;

import com.richard.library.context.AppContext;

import java.util.Locale;

/**
 * <pre>
 * Description : 调用TTS引擎文字转语音播报
 * Author : admin-richard
 * Date : 2020/11/20 15:45
 * Changelog:
 * Version            Date            Author              Detail
 * ----------------------------------------------------------------------
 * 1.0         2020/11/20 15:45     admin-richard         new file.
 * </pre>
 */
public class TTSManager {

    private TextToSpeech mSpeech;
    private boolean isInit;
    private Locale locale;

    private TTSManager() {
    }

    private static final class InstanceHolder {
        static final TTSManager instance = new TTSManager();
    }

    public static TTSManager getInstance() {

        return InstanceHolder.instance;
    }

    /**
     * 初始化TextToSpeech
     */
    public void init(Locale locale, InitCallback initCallback) {
        this.destroy();
        this.locale = locale;
        mSpeech = new TextToSpeech(AppContext.get(), status -> {
            if (status == TextToSpeech.SUCCESS) {
                int result = mSpeech.setLanguage(locale == null ? Locale.getDefault() : locale);
//                mSpeech.setPitch(1.0f); // 设置音调
//                mSpeech.setSpeechRate(1.5f); // 设置语速
                if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                    //语音合成初始化失败,不支持语种
                    isInit = false;
                } else {
                    isInit = true;
                }

                if (initCallback != null) {
                    initCallback.notifySettingLocaleTTSAvailable(isInit);
                }

                mSpeech.setOnUtteranceProgressListener(new UtteranceProgressListener() {
                    @Override
                    public void onStart(String utteranceId) {
                        //======语音合成 Start
                        if (initCallback != null)
                            initCallback.onStart(utteranceId);
                    }

                    @Override
                    public void onDone(String utteranceId) {
                        //======语音合成 Done
                        if (initCallback != null)
                            initCallback.onDone(utteranceId);
                    }

                    @Override
                    public void onError(String utteranceId) {
                        //======语音合成 Error
                        if (initCallback != null)
                            initCallback.onError(utteranceId);
                    }
                });
            }
        });
    }

    /**
     * 开始文字转语音并播报
     *
     * @param text 语音播报内容
     * @return 播报是否成功
     */
    public boolean speakText(String text) {
        if (!isInit) {
            //语音合成失败，未初始化成功
            this.init(locale, null);
            return false;
        }
        if (mSpeech != null) {
            int result = mSpeech.speak(text, TextToSpeech.QUEUE_ADD, null);
            return result == TextToSpeech.SUCCESS;
        }
        return false;
    }

    /**
     * 是否正在播报
     */
    public boolean isSpeaking() {
        if (mSpeech == null) {
            return false;
        }
        return mSpeech.isSpeaking();
    }

    /**
     * 验证当前设置的语言环境是否可用
     */
    public boolean isAvailable() {
        return this.isInit;
    }

    /**
     * 停止播报
     */
    public void stop() {
        if (mSpeech != null && mSpeech.isSpeaking()) {
            mSpeech.stop();
        }
    }

    /**
     * 关闭并销毁播报资源
     */
    public void destroy() {
        if (mSpeech != null) {
            mSpeech.stop();
            mSpeech.shutdown();
            isInit = false;
        }
    }

    /**
     * 回调
     */
    public static abstract class InitCallback extends UtteranceProgressListener {

        @Override
        public void onStart(String utteranceId) {

        }

        @Override
        public void onDone(String utteranceId) {

        }

        @Override
        public void onError(String utteranceId) {

        }

        /**
         * 通知是否支持当前的TTS语言环境
         *
         * @param isAvailable true：支持、false：不支持
         */
        public abstract void notifySettingLocaleTTSAvailable(boolean isAvailable);
    }
}
